/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Form;

import data.Cell;
import data.Column;
import data.Row;
import data.Table;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.plaf.TabbedPaneUI;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author GREENWIND
 */
public final class AddData extends JFrame implements ActionListener {

    Container frame = getContentPane();
    private JTable table;
    DefaultTableModel model;
    private String database;
    private String nametable;
    JScrollPane jsp_tb;
    JButton bt_Save;
    Object[] ob;

    public String tabName() {
        return nametable;
    }

    public String getDatabase() {
        return this.database;
    }

    public AddData(String title, String Database) {
        super(title);

        database = Database;
        nametable = title;
        InitTable();
        frame.add(jsp_tb);
        bt_Save = new JButton("Save");
        bt_Save.addActionListener(this);
        frame.add(bt_Save);

    }

    public void InitTable() {
        for (int i = 0; i < FormMain.ListDatabase.size(); i++) {
            if (FormMain.ListDatabase.get(i).GetName().equals(database)) {
                Table tb = FormMain.ListDatabase.get(i).getTable(nametable);
                ob = new Object[tb.getnCols()];
                for (int j = 0; j < tb.getnCols(); j++) {
                    ob[j] = tb.getListCol().get(j).getName();
                }

                model = new DefaultTableModel(ob, 50);
                table = new JTable(model);
                jsp_tb = new JScrollPane(table);
                if (!tb.getListRows().isEmpty()) {
                    ArrayList<Row> rows = (ArrayList<Row>) tb.getListRows();
                    for (int j = 0; j < rows.size(); j++) {
                        ArrayList<Cell> cells = (ArrayList<Cell>) rows.get(j).getListCell();
                        for (int k = 0; k < cells.size(); k++) {
                            table.getModel().setValueAt(cells.get(k).getData(),j, k);
                        }
                    }
                }
            }
        }
    }

    public ArrayList<Row> getDataRow() {
        ArrayList<Row> rows = new ArrayList<>();
        String _name;
        String _colname;
       // _colname = nametable;
        for (int i = 0; i < getRowDataCount(); i++) {
            ArrayList<Cell> cells = new ArrayList<>();
            for (int j = 0; j < table.getColumnCount(); j++) {
                _name = (String) table.getModel().getValueAt(i, j);
                _name = _name.toUpperCase();
                _colname = (String)ob[j];
                Cell cell = new Cell(_name, _colname);
                cells.add(cell);
            }
            Row row = new Row(cells, cells.size(), i);
            rows.add(row);
        }
        return rows;
    }
    public int getRowDataCount() {
        int count = 0;
        for (int i = 0; i < table.getModel().getRowCount(); i++) {
            for (int j = 0; j < table.getModel().getColumnCount(); j++) {
                if (table.getModel().getValueAt(i, j) != null) {
                    if (i > count) {
                        count = i;
                    }
                }
            }
        }
        return count+1;
    }

    public Boolean InputCorrect() {
        int type;
        for (int i = 0; i < getRowDataCount(); i++) {
            for (int j = 1; j < table.getModel().getColumnCount(); j++) {
                if (((String) table.getModel().getValueAt(i, j)) == null) {
                    return false;
                }
            }
        }
        for (int i = 0; i < FormMain.ListDatabase.size(); i++) {
            if (FormMain.ListDatabase.get(i).GetName().equals(database)) {
                for (int j = 0; j < table.getModel().getColumnCount(); j++) {
                    type = FormMain.ListDatabase.get(i).getTable(nametable).getListCol().get(j).getType();
                    if (type == 0) {
                        for(int k = 0; k < getRowDataCount();k++)
                        {
                            if(!Number.isNumber((String)table.getModel().getValueAt(k,j)))
                                return false;
                        }
                    }
                }
            }
        }
        return true;

    }
    public Boolean checkPrimary() {
        for (int i = 0; i < FormMain.ListDatabase.size(); i++) {
            if (FormMain.ListDatabase.get(i).GetName().equals(database)) {
                for (int j = 0; j < table.getModel().getColumnCount(); j++) {
                    Boolean isPrimaryKey = FormMain.ListDatabase.get(i).getTable(nametable).getListCol().get(j).getIsPrimary();
                    if(isPrimaryKey)
                    {
                       if(!checkIdenticalValue(j))
                           return false;
                    }
                }
            }
        }
        return true;
    }

    public Boolean checkIdenticalValue(int posCol) {
        for (int i = 0; i < getRowDataCount(); i++) {
            for (int j = i + 1; j < getRowDataCount(); j++) {
                if (((String) table.getModel().getValueAt(i, posCol)).
                        equals((String) table.getModel().getValueAt(j, posCol))) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == bt_Save) {
            if (!InputCorrect()) {
                JOptionPane.showMessageDialog(rootPane, "Fail: Missing value or type data", "Error", WIDTH);
                return;
            }
            if (getRowDataCount() == 0) {
                return;
            }
            if(!checkPrimary())
            {
                JOptionPane.showMessageDialog(rootPane, "Error: Identical value at column with primary key ", "Fail", WIDTH);
                return;
            }
            ArrayList<Row> rows = getDataRow();
            for (int i = 0; i < FormMain.ListDatabase.size(); i++) {
                if (FormMain.ListDatabase.get(i).GetName().equals(database)) {
                    FormMain.ListDatabase.get(i).getTable(nametable).getListRows().clear();
                    for (int j = 0; j < rows.size(); j++) {                   
                        FormMain.ListDatabase.get(i).getTable(nametable).insertRow(rows.get(j));
                    }

                }
            }
        JOptionPane.showMessageDialog(rootPane, "Đã nhập dữ liệu", "Success", WIDTH);
    }
}

    static class Number {

        static public Boolean isNumber(String s) {
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) < '0' || s.charAt(i) > '9') {
                    return false;
                }
            }
            return true;
        }
    }
}
